package com.xrtn.controller;

import com.xrtn.ApiVersion;
import com.xrtn.bean.InitDiseaseType;
import com.xrtn.service.ICommonService;
import com.xrtn.service.IDoctorService;
import com.xrtn.service.IRecordService;
import com.xrtn.utils.CodeEnum;
import com.xrtn.utils.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tu_pei on 2018/2/28.
 */
@Api("基础数据模块")
@Controller
@RequestMapping("/{version}/init")
public class InitController {

    @Autowired
    private ICommonService commonService;

    @Autowired
    private IRecordService recordService;

    @Autowired
    private IDoctorService doctorService;


    /**
     * @param @param  userId
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: selectDiseaseTypeList
     * @Description: 查询症状类型列表
     */
    @ApiOperation(value = "查询症状类型列表", notes = "查询症状类型列表")
    @RequestMapping(value = "/selectDiseaseTypeList", method = RequestMethod.POST)
    @ApiVersion(1)
    @ResponseBody
    public ResponseData selectDiseaseTypeList() {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        List<InitDiseaseType> diseaseList = commonService.selectDiseaseTypeList();

        List<Map<String, Object>> retList = new ArrayList<>();
        for (InitDiseaseType disease : diseaseList) {
            Map<String, Object> retMap = new HashMap<>();
            retMap.put("typeId", disease.getTypeId());
            retMap.put("isMain", disease.getIsMain());
            retMap.put("typeName", disease.getTypeName());
            retMap.put("typeStatus", disease.getTypeStatus());
            retMap.put("childList", commonService.selectDiseaseInfoList(disease.getTypeId()));
            retList.add(retMap);
        }

        data.setResults(retList);
        // 返回数据
        return data;
    }

    /**
     * @param @param  userId
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: selectDiseaseInfoList
     * @Description: 根据主症类型查询病症列表
     */
    @ApiOperation(value = "根据主症类型查询病症列表", notes = "根据主症类型查询病症列表")
    @RequestMapping(value = "/selectDiseaseInfoList", method = RequestMethod.POST)
    @ApiVersion(1)
    @ResponseBody
    public ResponseData selectDiseaseInfoList(Integer typeId) {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        data.setResults(commonService.selectDiseaseInfoList(typeId));
        // 返回数据
        return data;
    }

    /**
     * @param @param  userId
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: selectPointGroupList
     * @Description: 查询穴位分组信息
     */
    @ApiOperation(value = "查询穴位分组信息", notes = "查询穴位分组信息")
    @RequestMapping(value = "/selectPointGroupList", method = RequestMethod.POST)
    @ApiVersion(1)
    @ResponseBody
    public ResponseData selectPointGroupList() {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        data.setResults(commonService.selectPointGroupList());
        // 返回数据
        return data;
    }

    /**
     * @param @param  userId
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: selectInitServiceList
     * @Description: 查询推拿信息列表
     */
    @ApiOperation(value = "查询推拿信息列表", notes = "查询推拿信息列表")
    @RequestMapping(value = "/selectInitServiceList", method = RequestMethod.POST)
    @ApiVersion(1)
    @ResponseBody
    public ResponseData selectInitServiceList(Integer userId, Integer state, String serviceName) {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        data.setResults(commonService.selectInitServiceList(userId, state, serviceName));
        // 返回数据
        return data;
    }

    /**
     * @param @param  userId
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: selectInitGoodsList
     * @Description: 查询药品列表
     */
    @ApiOperation(value = "查询药品列表", notes = "查询药品列表")
    @RequestMapping(value = "/selectInitGoodsList", method = RequestMethod.POST)
    @ApiVersion(1)
    @ResponseBody
    public ResponseData selectInitGoodsList(Integer state, String goodsName) {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        data.setResults(commonService.selectInitGoodsList(state, goodsName));
        // 返回数据
        return data;
    }


    /**
     * @param @return
     * @return ResponseData
     * @throws
     * @Title: selectInitConclusionList
     * @Description: 查询诊断结论列表
     */
    @ApiOperation(value = "查询诊断结论列表", notes = "查询诊断结论列表")
    @RequestMapping(value = "/selectInitConclusionList", method = RequestMethod.POST)
    @ApiVersion(1)
    @ResponseBody
    public ResponseData selectInitConclusionList() {

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        data.setResults(commonService.selectInitConclusionList());
        // 返回数据
        return data;
    }

    /**
     * 查询特病题目列表
     *
     * @return
     */
    @ApiOperation(value = "查询特病题目列表", notes = "查询特病题目列表")
    @RequestMapping(value = "/selectSpecialList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ResponseData selectSpecialList() {
        ResponseData data = new ResponseData();
        data.setResults(recordService.selectRecordAll());
        return data;
    }

    /**
     * 查询选择中特病题目答案
     *
     * @return
     */
    @ApiOperation(value = "查询选择中特病题目答案", notes = "查询选择中特病题目答案")
    @RequestMapping(value = "/selectSpecialAnswerList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ResponseData selectSpecialAnswerList(Integer treatmentId) {
        ResponseData data = new ResponseData();
        Map<String,Object> map = recordService.selectByTreatmentId(treatmentId);
        if (null == map){
            map = new HashMap<>();
            map.put("specialRealScore",0);
            map.put("specialCheckScore",0);
            map.put("specialError",0);
        }
        map.put("infoIds",recordService.selectRecordAll(treatmentId));
        data.setResults(map);
        return data;
    }

    /**
     * 提交特病选项
     *
     * @return
     */
    @ApiOperation(value = "提交特病选项", notes = "提交特病选项")
    @RequestMapping(value = "/saveSpecial", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData saveSpecial(Integer userId, Integer patientId, Integer treatmentId, Integer doctorId, Integer specialId,
                                    Integer checkResult, Integer realResult, Integer error, String infoIds) {
        ResponseData responseData = new ResponseData();
        if (null == treatmentId || treatmentId == 0){
            responseData.setCodeEnum(CodeEnum.CODE_90001);
            return responseData;
        }

        recordService.deleteRecordAnswerByTreatmentId(treatmentId);

        if (null != infoIds && !infoIds.equals("[]")){
            Map<String, Object> params = new HashMap<>();
            params.put("userId", userId);
            params.put("patientId", patientId);
            params.put("treatmentId", treatmentId);
            params.put("doctorId", doctorId);
            JSONArray infoIdArrays = JSONArray.fromObject(infoIds);
            List<Integer> infoIdList = new ArrayList<>();
            for (int i = 0; i < infoIdArrays.size(); i++) {
                infoIdList.add(infoIdArrays.getInt(i));
            }
            params.put("infoIds", infoIdList);
            recordService.insertRecordAnswer(params);
        }

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("specialId", specialId);
        hashMap.put("specialCheckScore", checkResult);
        hashMap.put("specialRealScore", realResult);
        hashMap.put("specialError", error);
        hashMap.put("treatmentId", treatmentId);
        doctorService.updateByTreatmentId(hashMap);

        return responseData;
    }
}
